package com.crimsec.utils.snmp.model;

import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;

/**
 *
 * @author moroz
 */
public class Tutl {
    
    private static JAXBContext jaxb;

    public static void delTmpFiles(final String suffix) throws IOException {
        Boolean check_delete = Boolean.getBoolean("no.del.tmp");
        if (!check_delete) {
            File tmpd = new File(System.getProperty("java.io.tmpdir"));
            File[] tmpFileList = tmpd.listFiles(new FileFilter() {
                @Override
                public boolean accept(File pathname) {
                    return pathname.isFile() && pathname.getName().endsWith(suffix);
                }
            });
            for (File f : tmpFileList) {
                f.delete();
            }
        }
    }

    public static File createXmlTempFile(String prefix, String suffix) throws IOException {
        try {
            if (prefix.length() <= 3) {
                prefix += "____";
            }
            File result = File.createTempFile(prefix, suffix);
            return result;
        } catch (NullPointerException e) {
            throw new IOException("bad file name");
        }
    }
    
     public static JAXBContext getJAXBContext() throws JAXBException {
        if (jaxb != null) {
            return jaxb;
        } else {
            jaxb = JAXBContext.newInstance(Eventdata.class, Event.class, Mibent.class, Mibdb.class);
            return jaxb;
        }
    }
}
